package com.sise.aio和bio和nio.nio.零拷贝;


import com.sun.org.apache.xpath.internal.operations.String;
import io.netty.buffer.ByteBuf;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author idea
 * @data 2019/4/9
 */
public class NioDemo {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(1234);
        socketChannel.connect(inetSocketAddress);

        RandomAccessFile file =
                new RandomAccessFile(NioDemo.class.getClassLoader().getResource("test.txt").getFile(), "rw");
        FileChannel fileChannel=file.getChannel();
        fileChannel.transferTo(0,fileChannel.size(),socketChannel);
        ByteBuffer by=ByteBuffer.allocate(1024);
        socketChannel.read(by);
        System.out.println(by.array().toString());
        fileChannel.close();
        file.close();
        socketChannel.close();
    }
}
